import request from '@/utils/request'
import store from '../store'
import {
  getAppId
} from '@/utils/index'

function getClientMacro() {
  const macro = store.getters['page/clientMacro']
  if (getAppId()) {
    macro['appId'] = getAppId()
  }
  const clientMacro = {
    clientMacro: JSON.stringify(macro)
  }
  return clientMacro
}

// 获取大账号权限菜单
export function getAdminNav(query) {
  return request({
    url: '/init/getAdminNav',
    method: 'get',
    params: query,
    meta: {
      globalWarn: true // 接口报错后全局提醒，默认true
    }
  })
}

// 获取应用配置菜单
export function getSettingNav(query) {
  return request({
    url: '/init/getSettingNav',
    method: 'get',
    params: query,
    meta: {
      globalWarn: true // 接口报错后全局提醒，默认true
    }
  })
}

// 获取应用预览菜单
export function getAppPreviewNav(query) {
  return request({
    url: '/init/getAppPreviewNav',
    method: 'get',
    params: query,
    meta: {
      globalWarn: true // 接口报错后全局提醒，默认true
    }
  })
}

// 获取权限菜单-业务端【主框架允许时需要】
export function getAppNav(query) {
  return request({
    url: '/init/getAppNav',
    method: 'get',
    params: query,
    meta: {
      globalWarn: true // 接口报错后全局提醒，默认true
    }
  })
}

// 获取页面配置信息
export function getPageInfo(query) {
  return request({
    url: '/page/getPageInfo',
    method: 'get',
    params: query
  })
}

// 获取左侧区域数据
export function getLeftData(data) {
  const clientMacro = {
    clientMacro: JSON.stringify(store.getters['page/clientMacro'])
  }
  data = Object.assign({}, clientMacro, data)
  return request({
    url: '/page/getLeftData',
    method: 'post',
    data: data
  })
}

// 获取主列表数据
export function getMainData(data) {
  const clientMacro = {
    clientMacro: JSON.stringify(store.getters['page/clientMacro'])
  }
  data = Object.assign({}, clientMacro, data)
  return request({
    url: '/page/getMainData',
    method: 'post',
    data: data
  })
}

// 新增创建表单【废弃】
export function getAddFormInfo(data) {
  return request({
    url: '/form/add',
    method: 'post',
    data: data
  })
}

// 获取详情数据【废弃】
export function getEditFormInfo(data) {
  return request({
    url: '/form/getEditData',
    method: 'post',
    data: data
  })
}

// 获取表单详情
export function getFormData(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return request({
    url: '/form/getFormData',
    method: 'post',
    data: data
  })
}

// 获取表单详情-手工
export function getFormDataFromId(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return request({
    url: '/form/getFormDataByFormId',
    method: 'post',
    data: data
  })
}

// 获取表单详情
export function saveFormData(data) {
  // 删除REFDATA字段
  const newData = JSON.parse(JSON.stringify(data))
  delete newData.data.REFDATA
  return request({
    url: '/form/saveFormData',
    method: 'post',
    data: newData,
    meta: {
      loading: true
    }
  })
}

// 获取菜单详情
export function invokeMethod(data) {
  const newData = Object.assign({
    pageId: window.$CD?.pageId || '',
    appId: getAppId() || ''
  }, data)
  return request({
    url: '/invoke/method',
    method: 'post',
    data: newData
  })
}

// 获取通用数据
export function getDataSource(data) {
  const newData = Object.assign({
    pageId: window.$CD?.pageId,
    appId: getAppId() || ''
  }, data)
  return request({
    url: '/invoke/datasource',
    method: 'post',
    data: newData
  })
}

// 获取全部码表
export function getCodeData(data) {
  const newData = Object.assign({
    appId: getAppId() || ''
  }, data)
  return request({
    url: '/init/getCodeData',
    method: 'get',
    params: newData
  })
}

// 获取全部码表
export function getColLookupData(data) {
  return request({
    url: '/form/getColLookupData',
    method: 'post',
    data: data
  })
}

// 文件上传
export function fileUpload(data, file) {
  const form = new FormData()
  Object.keys(data).forEach(key => {
    form.append(key, data[key])
  })
  form.append('file', file)
  return request({
    url: '/file/upload',
    method: 'post',
    data: form
  })
}

// 保存排序
export function saveOrderId(data) {
  return request({
    url: '/form/saveOrderId',
    method: 'post',
    data: data,
    meta: {
      loading: true
    }
  })
}

// 获取用户信息
export function getCurrentUser(params) {
  return request({
    url: '/user/info',
    method: 'post'
  })
}

// 获取id
export function getOneId(query) {
  return request({
    url: '/invoke/getOneId',
    method: 'get',
    params: query
  })
}

// 获取表单详情
export function saveFormDataByFormId(data) {
  // 删除REFDATA字段
  const newData = JSON.parse(JSON.stringify(data))
  delete newData.data.REFDATA
  return request({
    url: '/form/saveFormDataByFormId',
    method: 'post',
    data: newData,
    meta: {
      loading: true
    }
  })
}

// 获取通用数据
export function getExtendEleDisplayList(params) {
  return request({
    url: '/page/getExtendEleDisplayList',
    method: 'get',
    params
  })
}

// 获取项目变更人员
export function getEmployeeList(params) {
  return request({
    url: '/connections/GetEmployeeList',
    method: 'get',
    params
  })
}

// 获取菜单信息
export function getNavInfo(params) {
  return request({
    url: '/init/getNavInfo',
    method: 'get',
    params: params
  })
}